/******************************************************************************
 *                       B O M B E R M A N  2 0 1 2
 * 
 * File Name: ServerSocket.java
 * 
 * COP 4331 Summer 2012 Project
 * 
 * Authors: Trevor Mancuso, Chris Chiarella, Zach Hoyler, Daniel Martin
 * 
 * 
 * Description: Each sound file has an array of Clips. The location of the next
 * clip in the array to use is stored in currentClip. By having many clips
 * available, we can have sounds overlap (if we used the same Clip, it would
 * just restart the sound and not overlap).
 * 
 ******************************************************************************/
package bomberman;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import javax.sound.sampled.*;

public enum Sound {
    
    EXPLOSION("explosion.wav"),
    DEATH("death.wav"),
    POWERUP("powerup.wav"),
    PLACEBOMB("placebomb.wav"),
    COUNTDOWN("countdown.wav"),
    SUDDENDEATH("suddendeath.wav"),
    DEATHBLOCK("deathblock.wav");
    
    private final int MAX_CLIPS = 8;
    
    private Clip[] clips;
    private int currentClip;
    
    Sound(String fileName)
    {
        currentClip = 0;
               
        try
        {
            clips = new Clip[MAX_CLIPS];
            
            for (int i = 0; i < MAX_CLIPS; i++)
            {
                //AudioInputStream inputStream = AudioSystem.getAudioInputStream(Bomberman.class.getResourceAsStream("/sounds/" + fileName));
                
                URL soundURL = getClass().getClassLoader().getResource("sounds/" + fileName);
                AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundURL);
                
                clips[i] = AudioSystem.getClip();                
                clips[i].open(inputStream);
            }
        }
        catch (UnsupportedAudioFileException ex)
        {
            ex.printStackTrace();
        }
        catch (LineUnavailableException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        } 
    }
    
    public void play()
    {
        clips[currentClip].setFramePosition(0);
        clips[currentClip].start();
        currentClip = (currentClip + 1) % MAX_CLIPS;
    }
}
